package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.doamin.cargo.ExtCproduct;
import com.itheima.doamin.cargo.ExtCproductExample;
import com.itheima.doamin.cargo.FactoryExample;
import com.itheima.service.cargo.ExtCproductService;
import com.itheima.service.cargo.FactoryService;
import com.itheima.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("cargo/extCproduct")
@Controller
public class ExtCproductController extends BaseController {
    @Reference
    ExtCproductService extCproductService;
    @Reference
    FactoryService factoryService;

    //所有
    //列表和
    @RequestMapping("list")
    public String list
            (String contractId, String contractProductId, @RequestParam(defaultValue ="1" ) int page, @RequestParam(defaultValue = "3") int size){
        //创建查询条件
        ExtCproductExample extCproductExample=new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(extCproductExample, page, size);
        request.setAttribute("page",pageInfo);
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);

        //创建查询条件
        FactoryExample factoryExample=new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        request.setAttribute("factoryList",factoryService.findAll(factoryExample));
        return "cargo/extc/extc-list";
    }

    @RequestMapping("edit")
    public String toEdit(ExtCproduct extCproduct){
        if (UtilFuns.isEmpty(extCproduct.getId())){
            Object contractProductId = request.getAttribute("contractProductId");
            extCproduct.setCompanyId(getCurrentUser().getCompanyId());
            extCproduct.setCompanyName(getCurrentUser().getCompanyName());
            //保存
            extCproductService.save(extCproduct);
        }else{
            //更新
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list?contractProductId="+extCproduct.getContractProductId()+"&contractId="+extCproduct.getContractId();
    }

    @RequestMapping("toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId){
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);
        //创建查询条件
        FactoryExample factoryExample=new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        request.setAttribute("factoryList",factoryService.findAll(factoryExample));
        request.setAttribute("contractProductId",extCproduct.getContractProductId());
        request.setAttribute("contractId",extCproduct.getContractId());
        return "cargo/extc/extc-update";
    }

    @RequestMapping("delete")
    public String toDelete(String contractId,String contractProductId,String id){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list?contractProductId="+contractProductId+"&contractId="+contractId;
    }
}
